/**
 * WizardPanel Class
 */
syslib.ui.WizardPanel = {
	Globals : {
		RegisteredWizards : []
	},
	Conversor : syslib.object.createClass()
};

syslib.ui.WizardPanel.Conversor.extend(syslib.ui.VisualTag);
syslib.ui.WizardPanel.Conversor.extend({
	prepare : function(){
		// STYLE
		syslib.loadStyleForComponent("wizard");
		
		// TAG ATTRIBUTES
		var attributes = this.__attributes;

		// CLASS ATTRIBUTES
		this.addProperty("object", "steps", {});
		this.addProperty("string", "title", "Wizard");
		this.addProperty("string", "actualStep", "");
		this.addProperty("string", "target", "blank");
		this.addProperty("string", "action", "none");
		this.addProperty("string", "id", "");
		this.addProperty("string", "nextLabel", "Next");
		this.addProperty("string", "previousLabel", "Previous");
		this.addProperty("string", "finishLabel", "Finish");
		this.addProperty("string", "cancelLabel", "Cancel");
		
		// SETS
		this.setId( (__valid(attributes.id)) ? attributes.id : "wizardpanel_" + Math.floor(Math.random() * 1001) );
		
		// RETURN
		return true;
	},

	execute : function(){
		// ATTRIBUTES
		var attributes = syslib.object.extend(this.__attributes, {
			"id" : this.getId(),
			"class" : "accordeon-main-container"
		});

		// JSON
		var elemtJSON = ["div", {"id" : this.getId(), "class" : "wizard-main-panel"},
							["div", {"class" : "wizard-header", "innerName" : "header"},
								["span", {"class" : "wizard-step-title", "innerName" : "stepTitle"}],
								["span", {"class" : "wizard-title", "innerName" : "title"}]
							],
							["form", {"name" : this.getId() + "_form", "innerName" : "dataForm"},
								["div", {"class" : "wizard-body", "innerName": "body"} ]
							],
							["div", {"class" : "wizard-footer", "innerName" : "footer"},
								["div", {"class" : "wizard-footer-left-buttons", "innerName" : "leftButtons"},
									["span", {"class" : "wizard-button-container", "innerName" : "cancelContainer"},
										["input", {"type" : "button", "class" : "wizard-button", "innerName" : "cancelButton"}]
									]
								],
								["div", {"class" : "wizard-footer-right-buttons", "innerName" : "rightButtons"},
									["span", {"class" : "wizard-button-container", "innerName" : "previousContainer"},
										["input", {"type" : "button", "class" : "wizard-button", "innerName" : "previousButton"}]
									],
									["span", {"class" : "wizard-button-container", "innerName" : "nextContainer"},
										["input", {"type" : "button", "class" : "wizard-button", "innerName" : "nextButton"}]
									],
									["span", {"class" : "wizard-button-container", "innerName" : "finishContainer"},
										["input", {"type" : "button", "class" : "wizard-button", "innerName" : "finishButton"}]
									]
								]
							]
						];

		// CREATE THE OBJECT
		var wizardPanel = this.createDOM(elemtJSON);

		// RETURN
		return wizardPanel;
	},

	finalize : function(){
		var attribs 	= this.__attributes;
		var references	= this.__refs;
		var wizards		= syslib.ui.WizardPanel.Globals.RegisteredWizards;

		for(var i = 0; i < wizards; i++){
			var wizard = wizards[i];
			wizard.parentNode.removeChild(wizard);
		}
		wizards.push(this.__finalElement);

		if(__valid(attribs.next)){this.setNextLabel(attribs.next);}
		if(__valid(attribs.previous)){this.setPreviousLabel(attribs.previous);}
		if(__valid(attribs.finish)){this.setFinishLabel(attribs.finish);}
		if(__valid(attribs.cancel)){this.setCancelLabel(attribs.cancel);}
		if(__valid(attribs.title)){this.setTitle(attribs.title);}
		if(__valid(attribs.action)){this.setAction(attribs.action);}
		if(__valid(attribs.target)){this.setTarget(attribs.target);}

		references.cancelButton.value		= this.getCancelLabel();
		references.finishButton.value		= this.getFinishLabel();
		references.previousButton.value		= this.getPreviousLabel();
		references.nextButton.value			= this.getNextLabel();

		references.body.innerHTML 			= this.__innerHTML;
		references.title.innerHTML			= this.getTitle();

		syslib.event.listen('click', references.cancelButton, function(){
			this.onEvent('cancel');
		}.applyTo(this));
		syslib.event.listen('click', references.finishButton, function(){
			this.onEvent('finish');
		}.applyTo(this));
		syslib.event.listen('click', references.nextButton, function(){
			this.onEvent('next');
		}.applyTo(this));
		syslib.event.listen('click', references.previousButton, function(){
			this.onEvent('previous');
		}.applyTo(this));
	},

	onEnterBody : function(){
		//syslib.ui.position.center(this.__finalElement, {
		//	setEvent : false
		//});
	},

	addStep : function(step){
		if(!__valid(step) || typeof step !== "object"){ return false; }

		var steps				= this.getSteps();
		var references 			= this.__refs;
		steps[step.getName()] 	= step;

		syslib.element.hide(step.__element);
		this.__refs.body.appendChild(step.__element);

		return true;
	},

	selectStep : function(name){
		var steps 		= this.getSteps();
		var references 	= this.__refs;
		var found 		= false;
		var elemt		= syslib.element;

		for(var stepName in steps){
			var step = steps[stepName];
			if(stepName !== name){
				elemt.hide(step.__element);
			} else {
				found = true;
				step.onEvent("select");
				this.setActualStep(name);

				(!step.getPreviousEnabled()) ? elemt.hide(references.previousContainer) : elemt.show(references.previousContainer);
				(!step.getNextEnabled()) ? 	elemt.hide(references.nextContainer) : elemt.show(references.nextContainer);
				(!step.getFinishEnabled()) ? elemt.hide(references.finishContainer) : elemt.show(references.finishContainer);
				(!step.getCancelEnabled()) ? elemt.hide(references.cancelContainer) : elemt.show(references.cancelContainer);

				references.stepTitle.innerHTML = step.getTitle();

				if(step.getAjax()){
					step.__element.innerHTML = '';
					new syslib.ajax.request(step.getPage(), {
						destiny: step.__element,
						form: (step.getPostData() == true) ? references.dataForm : false, 
						store : false
					});
				}

				elemt.show(step.__element);
			}
		}

		if(!found){ syslib.messages.error("Step '" + name + "' not found!"); }
		return found;
	},

	onEvent : function(eventName){
		var references	= this.__refs;
		var actualStep 	= this.getSteps()[this.getActualStep()];
		if(actualStep.onEvent(eventName) === false){
			return false;
		}

		switch(eventName){
			case "previous" :
				this.selectStep(actualStep.getPrevious());
			break;
			case "next" :
				if(actualStep.getValidateForm()){
					if(!syslib.form.validate(references.dataForm)){
						return false;
					} else {
						this.selectStep(actualStep.getNext());
					}
				} else {
					this.selectStep(actualStep.getNext());
				}
			break;
			case "finish" :
				if(this.getAction() !== "none"){
					new syslib.ajax.request(this.getAction(), {
						destiny : (this.getTarget() == "blank") ? false : this.getTarget(),
						evalScripts : true,
						store : false,
						form : references.dataForm
					});
				}
				this.__element.parentNode.removeChild(this.__element);
			break;
			case "cancel" :
				this.__element.parentNode.removeChild(this.__element);
			break;
			default:
				syslib.messages.error("Invalid wizard event name: " + eventName);
			break;
		}
	}
});